#include "warray/warray_improc.h"
#include "test.h"

#include <iostream>
#include "helper.h"

BOOST_AUTO_TEST_CASE( array_threshold )
{
	warp::arraynd<float,1> x(warp::extents[8]);
	warp::arraynd<float,1> y(x.dims());
	
	x.fill(0.0f);
	warp::threshold(x,1.0f,y);
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
	y = warp::threshold(x,1.0f);
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
	
	x.fill(2.0f);
	warp::threshold(x,1.0f,y);
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],2.0f);
	y = warp::threshold(x,1.0f);
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],2.0f);
	
	x.fill(1.0f);
	warp::threshold(x,1.0f,y);
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
	y = warp::threshold(x,1.0f);
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
	
	x.fill(0.5f);
	warp::threshold(x,1.0f,y,std::less<float>());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.5f);
	y = warp::threshold(x,1.0f,std::less<float>());
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.5f);
	
	x.fill(1.5f);
	warp::threshold(x,1.0f,y,std::less<float>());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
	y = warp::threshold(x,1.0f,std::less<float>());
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
	
	x.fill(1.0f);
	warp::threshold(x,1.0f,y,std::less<float>());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
	y = warp::threshold(x,1.0f,std::less<float>());
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],0.0f);
}

BOOST_AUTO_TEST_CASE( array_binarise )
{
	warp::arraynd<float,1> x(warp::extents[8]);
	warp::arraynd<bool,1>  y(x.dims());
	
	x.fill(0.0f);
	warp::binarise(x,1.0f,y);
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
	y = warp::binarise<bool>(x,1.0f);
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
	
	x.fill(2.0f);
	warp::binarise(x,1.0f,y);
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],true);
	y = warp::binarise<bool>(x,1.0f);
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],true);
	
	x.fill(1.0f);
	warp::binarise(x,1.0f,y);
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
	y = warp::binarise<bool>(x,1.0f);
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
	
	x.fill(0.5f);
	warp::binarise(x,1.0f,y,std::less<float>());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],true);
	y = warp::binarise<bool>(x,1.0f,std::less<float>());
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],true);
	
	x.fill(1.5f);
	warp::binarise(x,1.0f,y,std::less<float>());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
	y = warp::binarise<bool>(x,1.0f,std::less<float>());
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
	
	x.fill(1.0f);
	warp::binarise(x,1.0f,y,std::less<float>());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
	y = warp::binarise<bool>(x,1.0f,std::less<float>());
	BOOST_CHECK_EQUAL(x.dims(),y.dims());
	for(unsigned int i=0;i<y.dim(warp::dim1);++i)
		BOOST_CHECK_EQUAL(y[i],false);
}

